package cn.microservices.cloud.gateway.interpector.rquest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 参数的黑名单
 */
@Component
public class ParamsInterceptor {

    //考虑到循环遍历成本高，设置默认的最大值
    private final Map<String, String> PARAM_BLACK_LIST = new ConcurrentHashMap<>();
    private Logger log = LoggerFactory.getLogger(getClass());
    private int MAX_PARAMS_LENGTH = 10;


    /**
     * 设置参数的黑名单,仅支持get和post,不支持body里面的参数
     */
    public void addParamBlack(String paramKey, String paramVal) {
        if (PARAM_BLACK_LIST.size() >= MAX_PARAMS_LENGTH) {
            log.warn("黑名单参数已经达到达到最大数量={},禁止添加", MAX_PARAMS_LENGTH);
            return;
        }
        PARAM_BLACK_LIST.put(paramKey, paramVal);
    }

    /**
     * 删除参数的黑名单
     */
    public void delParamBlack(String paramKey) {

        PARAM_BLACK_LIST.remove(paramKey);
    }
}
